package com.easy.dn.ui.mainFragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.common.base.BaseFragment;
import com.easy.common.baserx.RxSubscriber;
import com.easy.common.commonutils.ImageLoaderUtils;
import com.easy.common.commonutils.ShareUtils;
import com.easy.common.commonwidget.RippleView;
import com.easy.dn.api.Api;
import com.easy.dn.api.ApiService;
import com.easy.dn.app.AppConstant;
import com.easy.dn.bean.HomeBean;
import com.easy.dn.bean.JobAddressBean;
import com.easy.dn.bean.ProfessionTypeBean;
import com.easy.dn.ui.main.activity.AddressTypeActivity;
import com.easy.dn.ui.main.activity.FeedbackActivity;
import com.easy.dn.ui.main.activity.ProfessionTypeActivity;
import com.easy.dn.ui.main.activity.SearchRecordsActivity;
import com.easy.dn.ui.main.activity.SearchResumeActivity;
import com.easy.dn.utils.MapHelper;
import com.easy.dn.utils.SerializableMap;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dn.easy.com.easyto.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * 单身狗就这样默默地看着你，一句话也不说。
 *
 * home fra
 * @author DN
 * @data  2017/5/2.
 **/

public class HomeFragment extends BaseFragment implements RippleView.OnRippleCompleteListener {
    @Bind(R.id.home_message)
    ImageView mHomeMessage;
    @Bind(R.id.tv_evaluationNum)
    TextView mTvEvaluationNum;
    @Bind(R.id.tv_remindNum)
    TextView mTvRemindNum;
    @Bind(R.id.tv_newResumeNum)
    TextView mTvNewResumeNum;
    @Bind(R.id.tv_newFeedBackNum)
    TextView mTvNewFeedBackNum;
    @Bind(R.id.txt_seachMainWord)
    TextView mTxtSeachMainWord;
    @Bind(R.id.tv_hrNum)
    TextView hrNum;
    @Bind(R.id.searchText)
    EditText mSearchText;
    @Bind(R.id.job_type_TextView)
    TextView mJobTypeTextView;
    @Bind(R.id.ll_job_type_TextView)
    LinearLayout mLlJobTypeTextView;
    @Bind(R.id.tv_profession_type)
    TextView mTvProfessionType;
    @Bind(R.id.ll_profession_type)
    LinearLayout mLlProfessionType;
    @Bind(R.id.tv_address_type)
    TextView mTvAddressType;
    @Bind(R.id.ll_address_type)
    LinearLayout mLlAddressType;
    @Bind(R.id.searchBtn)
    RippleView mSearchBtn;
    @Bind(R.id.searchAdvancedBtn)
    RippleView mSearchAdvancedBtn;
    @Bind(R.id.searchHistoryBtn)
    RippleView mSearchHistoryBtn;
    @Bind(R.id.scheduleBtn)
    RippleView mScheduleBtn;
    @Bind(R.id.cditupGrade)
    RippleView mCditupGrade;
    @Bind(R.id.company_cultivate)
    RippleView mCompanyCultivate;
    @Bind(R.id.Feedback)
    RippleView mFeedback;
    @Bind(R.id.job_fair)
    RippleView mJobFair;
    @Bind(R.id.downloadResumeBtn)
    RippleView mDownloadResumeBtn;
    @Bind(R.id.employee_insurance)
    RippleView mEmployeeInsurance;
    @Bind(R.id.investigate)
    RippleView mInvestigate;
    @Bind(R.id.avdImg)
    ImageView mAvdImg;
    private Map<String, Map<String, ProfessionTypeBean>> mMap;
    private SerializableMap mSerializableMap;
    private SerializableMap mAddressMap;

    @Override
    protected int getLayoutResource() {
        return R.layout.fra_home;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        mSearchBtn.setOnRippleCompleteListener(this);
        mFeedback.setOnRippleCompleteListener(this);
        mSearchHistoryBtn.setOnRippleCompleteListener(this);
        mSearchAdvancedBtn.setOnRippleCompleteListener(this);
        initRxBus();
        ImageLoaderUtils.display(getActivity(), mAvdImg, "http://www.madisoft.cn/mobile/images/hr_share.png", R.drawable.hr_share, R.drawable.hr_share);
        loadData();
    }

    private void initRxBus() {

        // rxBus --> 行业类别选中回调
        mRxManager.on(AppConstant.PROFESSIONTYPE, new Action1<SerializableMap>() {
            @Override
            public void call(SerializableMap serializableMap) {
                mSerializableMap = serializableMap;
                mMap = serializableMap.getMap();
                Map<String, ProfessionTypeBean> data = mMap.get("ISHOME");
                StringBuffer sb = new StringBuffer();
                for (String name : data.keySet()) {
                    sb.append(name + ",");
                }
                if (sb.length() > 0) {
                    mTvProfessionType.setText(sb.substring(0, sb.length() - 1).toString());
                } else {
                    mTvProfessionType.setText("");
                    mTvProfessionType.setHint(R.string.search_optional_number);
                }
            }
        });
        // rxBus --> 工作地点选中回调
        mRxManager.on(AppConstant.ADDRESSTYPEISHOME, new Action1<SerializableMap>() {
            @Override
            public void call(SerializableMap serializableMap) {
                mAddressMap = serializableMap;
                Map<String, List<JobAddressBean>> jobAddressMap = mAddressMap.getMap();
                List<JobAddressBean> jobAddressList = jobAddressMap.get(AppConstant.ADDRESSTYPEISHOME);
                StringBuffer sb = new StringBuffer();
                for (JobAddressBean bean : jobAddressList) {
                    sb.append(bean.getName() + ",");
                }
                if (sb.length() > 0) {
                    mTvAddressType.setText(sb.substring(0, sb.length() - 1).toString());
                } else {
                    mTvAddressType.setText("");
                    mTvAddressType.setHint(R.string.search_optional_number);
                }
            }
        });

    }

    private void loadData() {

        //首页数据加载 没使用MVP
        Map<String, String> map = MapHelper.getAccessMap();
        Api.getInstance().getProxy(ApiService.class).getHomeData(Api.getCacheControl(), map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new RxSubscriber<HomeBean>(getActivity(), false) {
            @Override
            protected void _onNext(HomeBean homeBean) {
                mTvEvaluationNum.setText(homeBean.getNewEvaluate());
                mTvRemindNum.setText(homeBean.getNewRemind());
                mTvNewResumeNum.setText(homeBean.getNotBrowse());
                mTvNewFeedBackNum.setText(homeBean.getNewFeedback());
                Integer surplusNum = homeBean.getSurplusNum() == null ? 0 : homeBean.getSurplusNum();
                hrNum.setText(String.valueOf(surplusNum));
            }

            @Override
            protected void _onError(String message) {
                showShortToast(message);
            }
        });
    }


    @OnClick({R.id.ll_job_type_TextView, R.id.ll_profession_type, R.id.ll_address_type, R.id.avdImg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_job_type_TextView:
                break;
            case R.id.ll_profession_type:
                //行业类别
                Bundle bundle = new Bundle();
                bundle.putSerializable("map", mSerializableMap);
                bundle.putString(AppConstant.PROFESSIONTYPE, "ISHOME");
                bundle.putInt("maxNum", 5);
                startActivity(ProfessionTypeActivity.class, bundle);
                break;
            case R.id.ll_address_type:
                //工作地点
                Bundle addressBundle = new Bundle();
                addressBundle.putSerializable("map", mAddressMap);
                addressBundle.putString(AppConstant.ADDRESSTYPE, AppConstant.ADDRESSTYPEISHOME);
                addressBundle.putInt("maxNum", 5);
                startActivity(AddressTypeActivity.class, addressBundle);
                break;
            case R.id.avdImg:
                // 分享
                ShareUtils.easyShowShare(getActivity(), "", "", "", "");
                break;
        }
    }

    @Override
    public void onComplete(RippleView rippleView) {
        switch (rippleView.getId()) {
            case R.id.home_message:

                break;
            case R.id.tv_evaluationNum:
                break;
            case R.id.tv_remindNum:
                break;
            case R.id.tv_newResumeNum:
                break;
            case R.id.tv_newFeedBackNum:
                break;
            case R.id.txt_seachMainWord:
                break;
            case R.id.searchBtn:
                startActivity(SearchResumeActivity.class);
                break;
            case R.id.searchAdvancedBtn:
                //                //高级搜索
//                Observable.create(new Observable.OnSubscribe<Object>() {
//                    @Override
//                    public void call(Subscriber<? super Object> subscriber) {
//                        TransApi api = new TransApi("20170816000074125", "CQJBhzJ4sztnSNf4HZxK");
//                        String query = "高度600米";
//                        subscriber.onNext(api.getTransResult(query, "auto", "en"));
//                        subscriber.onCompleted();
//                    }
//                }).subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Subscriber<Object>() {
//                            @Override
//                            public void onCompleted() {
//
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//
//                            }
//
//                            @Override
//                            public void onNext(Object o) {
//                                Log.i("DNLOG",o.toString());
//                            }
//                        });

                break;
            case R.id.searchHistoryBtn:
                //搜索记录
                startActivity(SearchRecordsActivity.class);
                break;
            case R.id.scheduleBtn:
                break;
            case R.id.cditupGrade:
                break;
            case R.id.company_cultivate:
                break;
            case R.id.Feedback:
                //意见反馈
                startActivity(FeedbackActivity.class);
                break;
            case R.id.job_fair:
                break;
            case R.id.downloadResumeBtn:
                break;
            case R.id.employee_insurance:
                break;
            case R.id.investigate:
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
